<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/5/9
 * Time: 16:32
 */

namespace app\api\controller;


use think\Db;

class Notice extends Base
{
    private $user_id;
    private $page;
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 系统消息列表
     * @return \think\response\Json
     * page    页码
     */
    public function systemNotice(){
        $notice = $this->mySystemNotice(1, 1);
        if($notice){
            // 已读/未读
            foreach($notice as $k => $v){
                if(strpos($v['notice_read_user'], ',' . $this->user_id . ',') !== false){
                    $notice[$k]['is_read'] = 1; // 已读
                }else{
                    $notice[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $notice]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 系统消息详情
     * @return \think\response\Json
     * notice_id    系统消息ID
     */
    public function systemNoticeDetail(){
        $notice_id = input('notice_id');
        $notice = Db::table('up_notice')
            ->field('notice_title, notice_content, notice_read_user, notice_time')
            ->where('notice_id', $notice_id)
            ->find();
        if(strpos($notice['notice_read_user'], ',' . $this->user_id . ',') === false){
            $read_user = $notice['notice_read_user'] . $this->user_id . ',';
            $res = Db::table('up_notice')
                ->where('notice_id', $notice_id)
                ->setField('notice_read_user', $read_user);
            if($res){
                return json(['status' => 200, 'msg' => $notice]);
            }else{
                return json(['status' => 0, 'msg' => '系统异常']);
            }
        }else{
            return json(['status' => 200, 'msg' => $notice]);
        }
    }
    
    /**
     * 清空系统消息
     * @return \think\response\Json
     */
    public function delAllNotice(){
        $notice_ids = $this->mySystemNotice(1, 2);
        if($notice_ids){
            try{
                foreach($notice_ids as $k => $v){
                    $del_user = $v['notice_del_user'] . $this->user_id . ',';
                    Db::table('up_notice')->where('notice_id', $v['notice_id'])->setField('notice_del_user', $del_user);
                }
                Db::commit();
                return json(['status' => 200, 'msg' => '清空成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => '清空失败']);
            }
            
        }else{
            return json(['status' => 200, 'msg' => '没有需要清空的消息']);
        }
    }
    
    /**
     * 未读消息数量&最新一条消息
     * @return \think\response\Json
     */
    public function unreadNum(){
        // 未读系统消息
        $unread = $this->mySystemNotice(2, 2);
        $num = count($unread);
        // 未读官方公告
        $whereG = [];
        $whereG['mgf_read_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        $whereG['mgf_del_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        $unread_gf = Db::table('up_match_gf')->where($whereG)->count();
        // 未读临时通知
        $whereL = [];
        $whereL['mfls_read_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        $whereL['mfls_del_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        $unread_ls = Db::table('up_match_ls')->where($whereL)->count();
        // 最新消息
        $newNotice = $this->mySystemNotice(1, 1);
        if($newNotice){
            $new = $newNotice[0];
        }else{
            $new = [];
        }
        return json(['status' => 200, 'msg' => $new, 'unread' => $num, 'unread_gf' => $unread_gf, 'unread_ls' => $unread_ls]);
    }
    
    /**
     * @param $type     1  列表   2 统计
     * @param $range    1  全部系统消息   2 未读系统消息
     * @return $this|false|\PDOStatement|string|\think\Collection
     */
    private function mySystemNotice($range, $type){
        $where['notice_del_user'] = ['notlike', '%,' . $this->user_id . ',%']; // 未被删除的
        if($range == 2){ // 未读
            $where['notice_read_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        }
        $where1['notice_user'] = [['=', -1], ['like', '%,' . $this->user_id . ',%'], 'or'];
        // 我是哪儿组委会工作人员
        $committee_worker = Db::table('up_committee_member')
            ->where('cm_match_user_id', $this->user_id)
            ->column('cm_committee_id');
        // 我报名了哪个组委会的赛事
        $match_ids = Db::table('up_match_apply_info')
            ->where('ma_user_id', $this->user_id)
            ->column('ma_match_id');
        $match_committee = Db::table('up_match')
            ->where('match_id', 'IN', $match_ids)
            ->column('match_committee_id');
        $committee_ids = array_merge($committee_worker, $match_committee);
        $my_committee = array_unique($committee_ids);
        
        $notice = Db::table('up_notice')
            ->where(function ($query) use ($where, $where1){
                $query->where('notice_type', 2)
                    ->where($where)
                    ->where($where1);
            });
        if($my_committee){
            $notice->whereOr(function ($query1) use ($where, $my_committee){
                $query1->where('notice_type', 1)
                    ->where($where)
                    ->where('notice_committee_id', 'IN', $my_committee);
            });
        }
        if($type == 1){
            $notice = $notice
                ->field('notice_id, notice_title, notice_content, notice_time, notice_read_user')
                ->order('notice_time desc')
                ->page($this->page, 10)
                ->select();
        }else{
            $notice = $notice
                ->field('notice_id, notice_del_user')
                ->select();
        }
        
        return $notice;
    }
}